//
//  NThemeDetailViewController.m
//  News
//
//  Created by zhangtao on 2018/10/15.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NThemeDetailViewController.h"

@interface NThemeDetailViewController ()
#define oneImgIdentifier @"oneImgIdentifier"
#define threeImgIdentifier @"threeImgIdentifier"
#define noImgIdentifier @"noImgIdentifier"

@property(nonatomic,strong)NThemDetailHeadView *headView;


@property(nonatomic,strong)NSMutableArray <NSMutableArray<NArticleModel *> *> *articles;
@property(nonatomic,strong)NSMutableArray <NSNumber *> *cps;
@property(nonatomic,assign)NSInteger cpIndex; //热门类型下标

@end

@implementation NThemeDetailViewController

-(instancetype)initWithStyle:(UITableViewStyle)style{
    return [super initWithStyle:UITableViewStyleGrouped];
}

-(void)setTopicalModel:(NTopicalModel *)topicalModel{
    _topicalModel = topicalModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"学做菜";
    
    _articles = [NSMutableArray arrayWithCapacity:0];
    _cps = [NSMutableArray arrayWithCapacity:0];
    _cpIndex = 0;
    for (int i = 0; i<3; i++) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
        [_cps addObject:[NSNumber numberWithInt:1]];
        [_articles addObject:arr];
        
    }
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"NMainOneImgCell" bundle:nil] forCellReuseIdentifier:oneImgIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"NMainThreeImgCell" bundle:nil] forCellReuseIdentifier:threeImgIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"NMainNoImgCell" bundle:nil] forCellReuseIdentifier:noImgIdentifier];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, tabBarHeight/2-14, 28, 28)];
    [btn setImage:[UIImage imageNamed:@"48-48slh"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    
//    [self requestTopicalSummary];
    [self requestArticles:theme_new];
}

-(void)requestTopicalSummary{
//    [[NHTTPManage shareInstance]afGet:HTTP_GET_TOPICAL_SUMMARY andParam:@{@"tid":_topicalID} success:^(id  _Nonnull responseObject) {
//        NSLog(@".");
//    } failture:^(id  _Nonnull error) {
//        NSLog(@".");
//    }];
}

-(void)rightClick{
    
}

-(NThemDetailHeadView *)headView{
    if (!_headView) {
        _headView = [[NSBundle mainBundle] loadNibNamed:@"NThemDetailHeadView" owner:self options:nil].firstObject;
        _headView.topicalModel = _topicalModel;
        WEAKSELF;
        _headView.themeDetailHeadCallBack = ^(THEME_DETAIL_ENUM kind) {
            if (kind == theme_new) {
                
                [weakSelf reloadUpdate:0];
                
            }else if (kind == theme_hot){
                
                [weakSelf reloadUpdate:1];
                
            }else if (kind == theme_wait){
                
                [weakSelf reloadUpdate:2];
                
            }else if (kind == theme_send) {
                NThemeSendArticleController *article = [[NThemeSendArticleController alloc]initWithNibName:@"NThemeSendArticleController" bundle:nil];
                [weakSelf.navigationController pushViewController:article animated:YES];
            }else if (kind == theme_focus){
                
            }
        };
    }
    return _headView;
}

-(void)reloadUpdate:(NSInteger)tag{
    self.cpIndex = tag;
    if (self.articles[tag].count==0) {
        [self requestArticles:tag];
    }else{
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
    }
}

-(void)requestArticles:(NSInteger)cpIndex{
    NSArray *urls = @[HTTP_GET_ARTICLE_BY_TOPICAL,HTTP_GET_HOT_24H,HTTP_GET_ARTICLE_WAIT];
    NSDictionary *param;
    if (cpIndex == theme_new) {
        param = @{@"tid":_topicalModel.ID,@"cp":_cps[cpIndex],@"ps":@"0"};
    }else if (cpIndex == theme_hot){
        param = @{@"topicalID":_topicalModel.ID,@"cp":_cps[cpIndex],@"num":@"20"};

    }else if (cpIndex == theme_wait){
        param = @{@"token":TOKEN,@"cuid":CUID, @"tids":_topicalModel.ID,@"cp":_cps[cpIndex],@"ps":@"0"};

    }
    [[NHTTPManage shareInstance]afGet:urls[cpIndex] andParam:param success:^(id  _Nonnull responseObject) {
        NSArray *arr = responseObject[@"Data"];
        
        for (NSDictionary *dic in arr) {
            NArticleModel *model = [[NArticleModel alloc]initWithDic:dic];
            [self.articles[cpIndex] addObject:model];
        }

        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
        
    } failture:^(id  _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0) return 0;
    return _articles[_cpIndex].count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0) return ACEWidth*3/7 + 45;
    return 0;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(section == 0)
    return self.headView;
    return nil;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NArticleModel *article = self.articles[_cpIndex][indexPath.row];
    NSString *coverPic = article.CoverPic;
    NSArray *coverPics = [coverPic componentsSeparatedByString:@","];
    if (coverPic.length == 0) {
        return ACEWidth*5/19;
    }else if (coverPic.length>0 && coverPics.count<3) {
        return ACEWidth*11/38;
    }else if (coverPics.count>=3){
        return ACEWidth*18/39;
    }
    
    
    
    if (indexPath.row == 0) {
        //380:110
        return ACEWidth*11/38;
    }
    
    if (indexPath.row == 1) {
        //390:180
        return ACEWidth*18/39;
    }
    
    if (indexPath.row == 2) {
        //380:120
        return ACEWidth*5/19;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //和后台确定以后使用articleType确定多图单图和无图
    NArticleModel *article = self.articles[_cpIndex][indexPath.row];
    NSString *coverPic = article.CoverPic;
    NSArray *coverPics = [coverPic componentsSeparatedByString:@","];
    if (coverPic.length == 0) {
        NMainNoImgCell *cell = [tableView dequeueReusableCellWithIdentifier:noImgIdentifier];
        cell.model = article;
        return cell;
    }else if (coverPic.length>0 && coverPics.count<3) {
        NMainOneImgCell *cell = [tableView dequeueReusableCellWithIdentifier:oneImgIdentifier];
        cell.model = article;
        return cell;
    }else if (coverPics.count>3){
        NMainThreeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:threeImgIdentifier];
        cell.model = article;
        return cell;
    }
    
    
    return nil;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
